<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加好友</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">添加好友</div>
                        <div class="layui-card-body">
                            <form class="layui-form" action="">
                                <input type="hidden" name="id" value="0">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">分组名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="searchWxName" lay-verify="searchWxName"
                                                   autocomplete="off"
                                                   placeholder="请输入手机号/微信ID/QQ号" value="" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">验证信息</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="content" lay-verify="content" autocomplete="off"
                                                   placeholder="请输入验证信息" value="" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">选择标签</label>
                                    <div class="layui-input-inline">
                                        <select id="tags" name="tags" lay-verify="tags" style="layui-input-inline"
                                                onchange="reloadList()">
                                            <option value="0">选择标签</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <a class="layui-btn" lay-submit lay-filter="formSubmit">提交</a>
                                        <a class="layui-btn layui-btn-primary" href="friend_manage.html">取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer'], function () {
            var $ = layui.$
                , form = layui.form
                , layer = layui.layer

            form.render()

            // 获取标签信息
            service.post('api/v1/wxmarks', {}).then((res) => {
                if (res.code === 0) {
                    var html = '<option value="0">选择标签</option>'
                    var groups = res.data
                    if (groups.length > 0) {
                        groups.forEach(o => {
                            html += '<option value=' + o.id + '>' + o.name + '</option>'
                        })
                    }
                    $('#tags').html(html)
                    form.render('select')
                } else {
                    layer.msg(res.msg, {offset: '45vh', icon: 5, time: 2000});
                }
            }).catch((err) => {
                layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
            })

            //自定义验证规则
            form.verify({
                content: function (value) {
                    if (value.length < 1) {
                        return '请输入验证信息';
                    }
                },
                searchWxName: function (value) {
                    if (value.length < 1) {
                        return '请输入手机号/微信ID/QQ号';
                    }
                },
                tags: function (value) {
                    if (value === '0') {
                        return '请选择标签';
                    }
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                service.post('api/v1/wxfriend/addfriendreq', {
                    tags: parseInt($('#tags').val()),
                    searchWxName: data.field.searchWxName,
                    content: data.field.content,
                }).then((response) => {
                    if (response.code === 200) {
                        layer.msg('发送成功', {offset: '45vh', icon: 1, time: 2000});
                    } else {
                        layer.msg(response.msg, {offset: '45vh', icon: 5, time: 2000});
                    }
                }).catch((err) => {
                    layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
                })
                return false;
            });
        });
    </script>
</div>
</body>
</html>
